﻿//-------------------------------------------------------------------------------------------
// <copyright file="AccountViewModel.cs" company="Russell Wilkins">
//     Copyright (c) Russell Wilkins 2014. All Rights Reserved.
// </copyright>
// <author>Russell Wilkins</author>
// <license href="license.txt">
//     The use of this software is governed by the Microsoft Public License
//     which is included with this distribution.
// </license>
//-------------------------------------------------------------------------------------------
namespace Ripple.TestClient.ViewModels
{
    using Ripple.Data;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;

    /// <summary>
    /// TODO: Describe the class
    /// </summary>
    public class AccountViewModel : ViewModel<Account>
    {
        public static readonly DependencyProperty AccountInfoProperty = DependencyProperty.Register("AccountInfo", typeof(AccountInfoViewModel), typeof(AccountViewModel));
        public static readonly DependencyProperty OffersProperty = DependencyProperty.Register("Offers", typeof(OffersViewModel), typeof(AccountViewModel));
        public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(AccountViewModel), new FrameworkPropertyMetadata(0, new PropertyChangedCallback(SelectedIndexChangedCallBack)));
        public static readonly DependencyProperty TransactionsProperty = DependencyProperty.Register("Transactions", typeof(TransactionsViewModel), typeof(AccountViewModel));
        public static readonly DependencyProperty TrustLinesProperty = DependencyProperty.Register("TrustLines", typeof(TrustLinesViewModel), typeof(AccountViewModel));

        private IRippleContext context;

        private bool offersLoaded;
        private bool transactionsLoaded;

        /// <summary>
        /// Initializes a new instance of the AccountViewModel class.
        /// </summary>
        public AccountViewModel(IRippleContext context)
        {
            this.context = context;
        }

        public AccountInfoViewModel AccountInfo
        {
            get { return (AccountInfoViewModel)GetValue(AccountInfoProperty); }
            set { SetValue(AccountInfoProperty, value); }
        }

        public OffersViewModel Offers
        {
            get { return (OffersViewModel)GetValue(OffersProperty); }
            set { SetValue(OffersProperty, value); }
        }

        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }

        public TransactionsViewModel Transactions
        {
            get { return (TransactionsViewModel)GetValue(TransactionsProperty); }
            set { SetValue(TransactionsProperty, value); }
        }

        public TrustLinesViewModel TrustLines
        {
            get { return (TrustLinesViewModel)GetValue(TrustLinesProperty); }
            set { SetValue(TrustLinesProperty, value); }
        }

        public void SetAccount(string accountId)
        {
            this.Model = this.context.Accounts.Get(accountId);
        }

        protected override void RefreshData()
        {
            this.DisplayName = this.Model.Id;

            if (this.Model != null)
            {
                this.AccountInfo = new AccountInfoViewModel(this.Model);
                this.TrustLines = new TrustLinesViewModel(this.context, this.Model);
            }
        }

        private static void SelectedIndexChangedCallBack(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            AccountViewModel vm = obj as AccountViewModel;
            int index = (int)args.NewValue;

            if (index == 1 && !vm.offersLoaded)
            {
                vm.Dispatcher.BeginInvoke(new Action(() => vm.LoadOffers()));
            }
            else if (index == 2 && !vm.transactionsLoaded)
            {
                vm.Dispatcher.BeginInvoke(new Action(() => vm.LoadTransactions()));
            }
        }

        private void LoadOffers()
        {
            this.Offers = new OffersViewModel(this.context, this.Model);
            this.offersLoaded = true;
        }

        private void LoadTransactions()
        {
            this.Transactions = new TransactionsViewModel(this.context, this.Model);
            this.transactionsLoaded = true;
        }
    }
}